package class02;

/**
 * 要求：给定两个个链表的头节点，如果相交返回交点；否则返回空
 * 1. 首先满足判断给定链表是否有环
 * 2. 都无环找交点
 * 3.一个有环一个无环不存在交点
 * 4.两个有环找交点
 */
public class Code05_FindFirstIntersectNode {
    static class Node {
        int value;
        Node next;

        Node(int value) {
            this.value = value;
        }

    }

    static Node getIntersectNode(Node head1, Node head2) {

        if (null == head1 || null == head2) {
            return null;
        }
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);

        if (null == loop1 && null == loop2) {
            return noLoop(head1, head2);
        }

        if (null != loop1 && null != loop2) {
            return bothLoop(head1, loop1, head2, loop2);
        }
        return null;
    }

    //step 1  链表是否有环，有则返回第一个交点否则返回空
    static Node getLoopNode(Node head) {
        if (null == head || null == head.next || null == head.next.next) {
            return null;
        }

        Node slow = head.next;
        Node fast = head.next.next;
        // 判断是否相交，不能采用值比较
        while (fast != slow) {

            if (null == fast.next || null == fast.next.next) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;

        }

        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;

    }

    // step 如果两个无环链表求交点，Y 结构
    static Node noLoop(Node head1, Node head2) {

        if (null == head1 || null == head2) {
            return null;
        }
//            int len1=0;
//            int len2=0;
        /**
         * 此处优化：
         * 常数项减少一个，利用step 的正负情况来比较长度
         */
        int step = 0;

        Node cur1 = head1;
        Node cur2 = head2;
        while (null != cur1.next) {
            step++;
            cur1 = cur1.next;
        }

        while (null != cur2.next) {
            step--;
            cur2 = cur2.next;
        }
        // 如果最后节点不相同，则不相交
        if (cur1 != cur2) {
            return null;
        }

        // 找到链表长的节点，让其先走多出的步长，然后两个链表齐步走

        cur1 = step > 0 ? head1 : head2;// 长链表
        cur2 = cur1 == head1 ? head2 : head1;
        while (Math.abs(step) != 0) {
            step--;
            cur1 = cur1.next;
        }

        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur2;


    }

    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        /**
         * 三种情况
         * 根据loop1 loop2 是否相等，如果相等证明交点在loop 节点之上，将loop 最为两个链表的终点 ，通过调整步长找交点
         * 如果loop1 loop2 不相等， 排除66 造型，一个链表从loop1 开始遍历，遍历到自己过程中没有碰到loop2 就能确定66 从而排除
         * 如果loop1 loop2 不相等， 且不是66 ，那么返回loop1 loop2 都可以；
         */

        Node cur1;
        Node cur2;
        if (loop1 == loop2) {

            cur1 = head1;
            cur2 = head2;
            int step = 0;

            while (cur1 != loop1) {
                step++;
                cur1 = cur1.next;
            }

            while (cur2 != loop2) {
                step--;
                cur2 = cur2.next;
            }

            cur1 = step > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            int n = Math.abs(step);
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }

            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur2;


        } else {
            cur1 = loop1.next;
            while (cur1 != loop1) {

                if (cur1 == loop2) {
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null;
        }


    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }


}
